//
//  UIPlaceHolderTextView.m
//  DD
//
//  Created by HaiJiao Chen on 12-8-5.
//  Copyright (c) 2012年 Kaixin. All rights reserved.
//

#import "UIPlaceHolderTextView.h"

@interface UIPlaceHolderTextView()
-(void)__updatePlaceHolderTextView;
@end

@implementation UIPlaceHolderTextView
@synthesize temp;

-(void)setTemp:(UIEdgeInsets)_temp
{
    temp = _temp;
    [self setContentInset:temp];
}
//防止系统乱改Inset
-(void)setContentInset:(UIEdgeInsets)insets
{
    //	insets.bottom = 10;
    ////	insets.top = 7;
    //    insets.top = 0;
	[super setContentInset:temp];
}


@synthesize placeholderOffset;
- (void)setPlaceholderOffset:(CGPoint)_placeholderOffset
{
    placeholderOffset = _placeholderOffset;
    [self __updatePlaceHolderTextView];
}

-(void)__updatePlaceHolderTextView
{
    CGRect rc = label_placeHolder.frame;
    rc.origin = CGPointMake(placeholderOffset.x, placeholderOffset.y);
    rc.size.width = self.frame.size.width-rc.origin.x;
    label_placeHolder.frame = rc;
}
-(void)initlize
{
    placeholderOffset = CGPointZero;
    temp = UIEdgeInsetsZero;
    [super setContentInset:temp];
    label_placeHolder = [[UILabel alloc] initWithFrame:CGRectZero];
    label_placeHolder.font = self.font;
    label_placeHolder.textColor = [UIColor lightGrayColor];
    label_placeHolder.backgroundColor = [UIColor clearColor];
    label_placeHolder.text = @"a";
    [label_placeHolder sizeToFit];
    label_placeHolder.text = nil;
    label_placeHolder.userInteractionEnabled = NO;
    [self addSubview:label_placeHolder];
    self.text = label_placeHolder.text;
    [self __updatePlaceHolderTextView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(valueChanged:) name:UITextViewTextDidChangeNotification object:nil];
}




-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [label_placeHolder release];
    [super dealloc];
}
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initlize];
    }
    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self initlize];
    }
    return self;
}
-(void)setPlaceholder:(NSString *)placeholder
{
    label_placeHolder.text = placeholder;
}

- (NSString*)placeholder
{
    return label_placeHolder.text;
}

-(void)valueChanged:(NSNotification*)notification
{
    label_placeHolder.hidden = self.text.length > 0;
}
- (void)setText:(NSString *)text
{
    [super setText:text];
    label_placeHolder.hidden = self.text.length > 0;
}
@end
